package org.blackjuly.sign.util;

import java.util.Collection;
import java.util.Iterator;

public class StringUtil {
	/**
	 * 判断字符串是否为null或者空字符串
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}
	
	/**
	 * 判断字符串是否不为null或者空字符串
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}
	
	/**
	 * 如果传入的对象为空则返回定义的默认值，否则返回原始对象字符串
	 */
	public static String defaultString(Object obj, String defualtValue) {
		if (obj == null || obj.toString().length() == 0) {
			return defualtValue;
		}
		return obj.toString();
	}
	
	/**
	 * 在集合里插入指定字符串后拼接返回
	 */
	public static <E> String join(Collection<E> coll, String str) {
		StringBuilder builder = new StringBuilder();
		if (coll.size() == 0)
			return "";
		
		Iterator<E> iter = coll.iterator();
		if (iter.hasNext()) {
			builder.append(iter.next().toString());
		}
		while (iter.hasNext()) {
			builder.append(str).append(iter.next().toString());
		}
		return builder.toString();
	}
	
	/**
	 * 给定字符串重复n次输出返回
	 */
	public static String repeat(String str, int time) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < time; i++) {
			builder.append(str);
		}
		return builder.toString();
	}
	
	/**
	 * 首字母大写返回
	 */
	public static String letter(String str) {
		if (isEmpty(str))
			return "";
		
		if (str.length() == 1)
			return str.toUpperCase();
		
		return str.substring(0, 1).toUpperCase() + str.substring(1, str.length());
	}
}